using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PietschSoft.VE;

namespace SecurityLinkUp.Admin
{
    public partial class CommunityAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // before we do anything, ensure that the user has the right
            // role to perform anything on this page
            Security.CheckAdminUser(this);

            if (Roles.IsUserInRole("SuperAdmin"))
            {
                AdminModeChk.Visible = true;
                DeleteBtn.Visible = true;
            }

            if (!Page.IsPostBack)
            {
                // add the status items
                foreach (string s in System.Enum.GetNames(typeof(CommonData.Status)))
                {
                    StatusList.Items.Add(s);
                }

                // populate the communities list
                ArrayList communities = CommunityData.GetUserAdminCommunities((Guid)Membership.GetUser().ProviderUserKey);
                CommunitiesSelect.Items.Add("");
                foreach (Community c in communities)
                {
                    ListItem itm = new ListItem(c.name + "  [" + c.status + "]", c.uid.ToString());
                    CommunitiesSelect.Items.Add(itm);
                }
                
                // start off with the status list disabled until we have a selected Community
                StatusList.Enabled = false;
            }
        }

        protected void AdminModeChk_CheckedChanged(object sender, EventArgs e)
        {
            // clear the old items
            CommunitiesSelect.Items.Clear();

            // repopulate the Communities list
            ArrayList Communities;
            if (!AdminModeChk.Checked)
            {
                Communities = CommunityData.GetUserAdminCommunities((Guid)Membership.GetUser().ProviderUserKey);
            }
            else
            {
                Communities = CommunityData.GetCommunities(CommonData.Status.ANY);
            }
            CommunitiesSelect.Items.Add("");
            foreach (Community c in Communities)
            {
                ListItem itm = new ListItem(c.name + "  [" + c.status + "]", c.uid.ToString());
                CommunitiesSelect.Items.Add(itm);
            }

            // clear the old data
            ClearData();
        }

        protected void Communitieselect_SelectedIndexChanged(object sender, EventArgs e)
        {
            // clear the old data
            ClearData();

            // if the blank "start position" Community is selected, exit now
            if (CommunitiesSelect.SelectedValue == "")
            {
                return;
            }

            // enable the status dropdown
            StatusList.Enabled = true;

            // get the community and set the basic properties
            Community c = CommunityData.GetCommunity(new Guid(CommunitiesSelect.SelectedValue));
            CommunityName.Text = c.name;
            CommunityDescritpion.Text = c.description;
            CommunityReason.Text = c.statusnotes;

            // set the Community status
            ListItem Communityitm = StatusList.Items.FindByValue(c.status.ToString());
            if (Communityitm != null)
            {
                int i = StatusList.Items.IndexOf(Communityitm);
                StatusList.SelectedIndex = i;
            }
            else
            {
                StatusList.SelectedIndex = 0;
            }

        }

        // clear all the data from the form
        protected void ClearData()
        {
            // set the Community data
            CommunityName.Text = "";
            CommunityDescritpion.Text = "";
            CommunityReason.Text = "";
            StatusList.SelectedIndex = 0;

            // disable the status dropdown
            StatusList.Enabled = false;

            CommunityErrorMessage.Text = "";
        }

        protected void SaveCommunityBtn_Click(object sender, EventArgs e)
        {
            // if the blank "start position" Community is selected, exit now
            if (CommunitiesSelect.SelectedValue == "")
            {
                return;
            }

            // create a Community object
            Community c = new Community(new Guid(CommunitiesSelect.SelectedValue),
                                        CommunityName.Text,
                                        CommunityDescritpion.Text,
                                        (CommonData.Status)Enum.Parse(typeof(CommonData.Status),StatusList.SelectedValue, true),
                                        CommunityReason.Text);


            // save the Community to the database
            string result = CommunityData.SaveCommunityToDB(c);
            if (result == "")
            {
                CommunityErrorMessage.Text = "Successfully saved.";
            }
            else
            {
                CommunityErrorMessage.Text = result;
            }
        }

        protected void ConfirmDeleteBtn_Click(object sender, EventArgs e)
        {

        }
    }
}
